<template>
    <div class="audit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="600px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <div class="mb-4">
                <h4 class="text-lg font-medium mb-2">墓地信息</h4>
                <el-descriptions :column="2" border>
                    <el-descriptions-item label="逝者姓名" label-width="100px">{{ formData.name }}</el-descriptions-item>
                    <el-descriptions-item label="墓地别名" label-width="100px">{{ formData.alias }}</el-descriptions-item>
                </el-descriptions>
            </div>
            
            <el-form ref="formRef" :model="auditData" label-width="100px" :rules="formRules" v-if="mode === 'audit'">
                <el-form-item label="审核结果" prop="audit_status">
                    <el-radio-group v-model="auditData.audit_status">
                        <el-radio :label="1" size="large">
                            <span class="text-green-600">审核通过</span>
                        </el-radio>
                        <el-radio :label="2" size="large">
                            <span class="text-red-600">审核拒绝</span>
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            
            <!-- 查看模式 -->
            <div v-if="mode === 'view'">
                <h4 class="text-lg font-medium mb-2">审核结果</h4>
                <el-descriptions :column="1" border>
                    <el-descriptions-item label="审核状态">
                        <el-tag 
                            :type="getAuditStatusType(formData.is_disable)"
                            effect="light"
                        >
                            {{ getAuditStatusText(formData.is_disable) }}
                        </el-tag>
                    </el-descriptions-item>
                    
                    <el-descriptions-item label="审核时间">
                        {{ timeFormat(formData.audit_time, 'yyyy-mm-dd hh:MM:ss') }}
                    </el-descriptions-item>
                </el-descriptions>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="graveyardAuditEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiGraveyardCheck } from '@/api/graveyardManage/list'
import { timeFormat } from '@/utils/util'
import { reactive, ref, shallowRef, computed } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('audit') // audit: 审核模式, view: 查看模式

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value === 'audit' ? '墓地审核' : '查看审核结果'
})

// 墓地基本信息
const formData = reactive<{
    id: string,
    [key: string]: any
}>({
    id: ''
})

// 审核数据
const auditData = reactive({
    id: '',
    audit_status: 1,
})

// 表单验证
const formRules = reactive<any>({
    audit_status: [{
        required: true,
        message: '请选择审核结果',
        trigger: ['change']
    }]
})

// 获取审核状态文本
const getAuditStatusText = (status: number) => {
    const statusMap: Record<number, string> = {
        0: '待审核',
        1: '审核通过',
        2: '审核拒绝'
    }
    return statusMap[status] || '未知状态'
}

// 获取审核状态标签类型
const getAuditStatusType = (status: number) => {
    const typeMap: Record<number, string> = {
        0: 'warning',
        1: 'success',
        2: 'danger'
    }
    return typeMap[status] || 'info'
}

// 设置表单数据
const setFormData = async (data: Record<any, any>) => {
    for (const key in data) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    // 设置审核数据ID
    auditData.id = data.id
}

// 提交审核
const handleSubmit = async () => {
    if (mode.value === 'view') {
        popupRef.value?.close()
        return
    }
    
    await formRef.value?.validate()
    
    // 调用审核接口
    await apiGraveyardCheck({
        id: auditData.id,
        is_disable: auditData.audit_status,
    })
    
    popupRef.value?.close()
    emit('success')
}

// 打开弹窗
const open = (type = 'audit') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    // 重置审核数据
    Object.assign(auditData, {
        id: '',
        audit_status: 1,
        audit_remark: ''
    })
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>

<style scoped>
.audit-popup :deep(.el-dialog) {
    max-height: 90vh;
    overflow-y: auto;
}

.audit-popup :deep(.el-descriptions__label) {
    font-weight: 500;
}
</style>